import { defineStore } from 'pinia'
//单独存放Store的名字  $id
import { Names } from './store-name'

type User = {
  name: string
  age: number
}
let res: User = {
  name: '孙悟空',
  age: 999,
}
const Login = (): Promise<User> => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve({
        name: '孙悟空',
        age: 999,
      })
    }, 2000)
  })
}

export const useTestStore = defineStore(Names.TEST, {
  state: () => {
    return {
      user: <User>{},
      name: '飞机',
    }
  },
  //类似computed 修饰一些值
  getters: {
    newName():string {
      return `new--${this.name}-${this.getUserAge}`
    },
    getUserAge():number {
      return this.user.age
    }
  },
  // 类似methods 可以同步  异步  提交state
  actions: {
    setName() {
      this.name = '孙悟空'
    },
    async setUser() {
      this.user = await Login()
      this.setName()
    },
  },
})
